//
//  ClubIntegralDetailViewController.swift
//  77Club
//
//  Created by Apple on 2024/9/11.
//

import UIKit

class ClubIntegralDetailViewController: ClubBaseViewController, RaeAnimationProtocol {

    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.register(ClubIntegralDetailCell.self,ClubIntegralDetailWebCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.estimatedRowHeight = 400.0.Scale
        return tableView
    }()
    private var bottomView = ClubIntegralDetailBottomView()
    var collectId: String = "" {
        didSet{
            bottomView.collectId = collectId
        }
    }
    private var dataModel = ClubMallListModel()
    var goodsId: String = "" {
        didSet{
            loadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            tableView
            bottomView
        }
        tableView.leading(0).trailing(0).top(0).bottom(.safe_bottom + 48.0.Scale)
        bottomView.leading(0).trailing(0).bottom(0).height(.safe_bottom + 48.0.Scale)
        
        bottomView.buyBlock = {
            let alrtView = ClubIntegralBuyAlertView()
            alrtView.model = self.dataModel
            alrtView.toBuyDetailBlock = {
                
            }
            self.entryAnimate.animation(show: alrtView)
        }
    }
    
    private func loadData(){
        Task {
            let res = await network.request(.mallDetail(goodsId: goodsId),type: ClubMallListModel.self)
            if res.isSuccess, let model = res.model {
                self.dataModel = model
                self.bottomView.model = model
                self.tableView.reloadData()
            }
        }
    }
}

//MARK: - UITableViewDelegate, UITableViewDataSource
extension ClubIntegralDetailViewController: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ClubIntegralDetailCell", for: indexPath) as! ClubIntegralDetailCell
            cell.model = self.dataModel
            cell.backBlock = {
                self.navigationController?.popViewController(animated: true)
            }
            return cell
        }
        let cell = tableView.dequeueReusableCell(withIdentifier: "ClubIntegralDetailWebCell", for: indexPath) as! ClubIntegralDetailWebCell
        cell.model = self.dataModel
        cell.updateDataBlock = { md in
            self.dataModel = md
            self.tableView.reloadData()
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
//        let detailVC = HWGameDetailController()
//        detailVC.gameId = dataModel?[indexPath.row].id ?? ""
//        self.navigationController?.pushViewController(detailVC, animated: true)
    }

}
